﻿var m_project_id = '';
var m_xkzh = '';
var m_obj = null;
var m_mqks = null;
var sfmap = null;
var scales = [1 / 553, 1 / 1106, 1 / 2212, 1 / 4514, 1 / 9028, 1 / 18055, 1 / 36111, 1 / 72223, 1 / 144447, 1 / 288895, 1 / 577791, 1 / 1155583, 1 / 2311166, 1 / 4622332, 1 / 9244664];

{
    var r = new RegExp("(^|(.*?\\/))(kqfw.js)(\\?|$)"),
	s = document.getElementsByTagName('script'),
	src, m, baseurl = "";
    for (var i = 0, len = s.length; i < len; i++) {
        src = s[i].getAttribute('src');
        if (src) {
            var m = src.match(r);
            if (m) {
                baseurl = m[1];
                break;
            }
        }
    }
    inputScript(baseurl + 'config.js'); btnJSMJ
}
function inputScript(inc) {
    var script = '<' + 'script type="text/javascript" src="' + inc + '"' + '><' + '/script>';
    document.writeln(script);
}
function DTDW() {
    var qks = CopyQKS(m_mqks);
    for (var i = 0; i < qks.length; i++) {
        qks[i].length = qks[i].length - 1;
    }


//    if (parent.parent.parent.frames["myFrame_myWin_yztzs"] == null) {
//        parent.parent.parent.afterYztLoaded = function () {
//            parent.parent.parent.frames["myFrame_myWin_yztzs"].locateByPoints([[[29463426.457919367, 5095055.50776774], [29511606.10632875, 5091077.371660544], [29505417.89460644, 5034057.420790725], [29431601.369061794, 5041571.677882097]]], ["FID", "FFF", "FDF"], ["ASDF", "ASDF", "ASDF"]);
//        }
//        parent.parent.parent.openyzt();

//    }
//    else {
//        parent.parent.parent.openyzt();
//        parent.parent.parent.frames["myFrame_myWin_yztzs"].locateByPoints([[[29463426.457919367, 5095055.50776774], [29511606.10632875, 5091077.371660544], [29505417.89460644, 5034057.420790725], [29431601.369061794, 5041571.677882097]]], ["FID", "FFF", "FDF"], ["ASDF", "ASDF", "ASDF"]);
    //    }
    if (OpenWindow == null) {
        OpenWindow = window.open("../../Map/Map.aspx?p=[[[29463426.457919367, 5095055.50776774], [29511606.10632875, 5091077.371660544], [29505417.89460644, 5034057.420790725], [29431601.369061794, 5041571.677882097]]]&fields=[采矿权人,矿种]&values=[李某,煤]", "newwindow");
        OpenWindow.onload = function () { alert("我是kqfw里的加载完成事件"); };
        OpenWindow.afterLoaded = afterYztLoaded;//  function () { alert("我是kqfw里的加载完成事件"); };
        OpenWindow.locateByPoints([[[29463426.457919367, 5095055.50776774], [29511606.10632875, 5091077.371660544], [29505417.89460644, 5034057.420790725], [29431601.369061794, 5041571.677882097]]], ["FID", "FFF", "FDF"], ["ASDF", "ASDF", "ASDF"]);
    }
    else {
        OpenWindow.locateByPoints([[[29463426.457919367, 5095055.50776774], [29511606.10632875, 5091077.371660544], [29505417.89460644, 5034057.420790725], [29431601.369061794, 5041571.677882097]]], ["FID", "FFF", "FDF"], ["ASDF", "ASDF", "ASDF"]);
        OpenWindow.focus();
    }
}
function afterYztLoaded()
{ alert("我是kqfw里的加载完成事件"); }
var OpenWindow = null;
$("document").ready(function () {

    m_project_id = $.query.get("PROJECT_ID");
    var SLLX = $.query.get("SLLX");
    if (SLLX != "1") {
        $(".m_btn").css("display", "none");
        $("#btnJSMJ").css("display", "inline-block");
    }
    /*地图先不要
    sfmap = new sfMap("id_div_map");
    sfmap.showToolbar();
    var width = $("#id_div_map").width();
    sfmap.movePanZoomControl(width - 80, 0);
    sfmap.enableMouseWheel();

    sfmap.setScales(scales); //设置固定比例尺数组
    sfmap.afterBaseLayersInit = function () {
        sfmap.addLayer(kqfwdturl);

    }
    sfmap.addLayer(kqfwdturl0, "底图", 0, true);
    */
    if (m_project_id != null && m_project_id != '') {
        var dbalias = "CKQDJK";
        var tablename = "采矿申请登记";
        if (typeof (kqfwBusinessTableName) != "undefined") {
            tablename = kqfwBusinessTableName;
        }
        var condittion = "PROJECT_ID=\\\'" + m_project_id + "\\\'";
        m_obj = new SmObjectData("CKQDJK", tablename);
        m_obj.GetFirstSmObject(condittion);
        //        if (tablename == "采矿转让登记") {
        //            condittion = "XKZH=\\\'" + m_obj.GetValue("YXKZH") + "\\\'";
        //            m_obj = new SmObjectData("CKQDJK", "采矿申请登记");
        //            m_obj.GetFirstSmObject(condittion);
        //            $(":button").css("display", "none");
        //        }
        InitSQB();
    }
    $("table input[value=编辑图形]").click(EditZB);

});
function InitSQB() {
    var obj = m_obj;
    for (i = 0; i < obj.Data.Data.length; i++) {
        var key = obj.Data.Data[i].Key;
        var value = obj.Data.Data[i].Value;
        if ($("input[name='txt" + key + "']") != null) {
            $("[name='txt" + key + "']").val(value);
        }
        if (key == "区域坐标") {
            $("tr[tag='useradd']").remove();
            var values = value.split(',');
            m_mqks = GetZBC(values);
            var qks = CopyQKS(m_mqks);


            addToTableAndMap(qks);
        }
    }
}

function addToTableAndMap(qks) {
    var j = 0;
    $("#id_zb_table tr[tag='useradd']").remove();
    if (qks != null && qks.length > 0) {
        var x, y;
        for (var ii = 0; ii < qks.length; ii++) {
            var zbs = qks[ii];
            if (zbs != null && zbs.length > 0) {
                x = zbs[0].x; y = zbs[0].y;
                for (var j = 0; j < zbs.length; j++) {
                    var str = '<tr tag="useradd"><td >' + zbs[j].gdh + '</td><td >' + zbs[j].y + '</td>\
                                            <td>' + zbs[j].x + '</td></tr>';
                    $("#id_zb_table").append(str);
                }
            }
        }
        $("#id_zb_table tr[tag='useradd']").click(function () { $("#id_zb_table tr.selected").removeClass("selected"); $(this).addClass("selected"); });

        if (x > 4200000 && x < 43000000) {
            $("#id_sel_tyfs option[tag='3dd']").attr("selected", true);
        }
        else if (180 > x > 0) {
            $("#id_sel_tyfs option[tag='jwd']").attr("selected", true);
        }
        else if (21000000 > x > 20000000) {
            $("#id_sel_tyfs option[tag='6dd']").attr("selected", true);
        }

        //投影转换
        //qks = ConvertZB(qks)
        //AddGeometryEx(qks, "", "kqfw");
    }
}


function AddGeometryEx(qks, color, tag) {
//    sfmap.clearMapsByTag(tag);
//    sfmap.addGeometryEx(qks, color, tag);
}
function GetZBC(kqfws) {
    var listqk = new Array();
    var qkcount = 0;
    var list = new Array();
    var ds = parseInt(kqfws[1]);
    var curpos = 2;
    var xh = 1;
    var i = 0;
    while (ds > 0) {
        for (i = 0; i < ds; i++) {
            var row = new ZBROW();
            row.xh = xh;
            row.gdh = kqfws[curpos];
            row.y = kqfws[curpos + 1];
            row.x = kqfws[curpos + 2];
            row.ktbs = "";
            row.xz = "";
            xh++;
            curpos += 3;
            list[list.length] = row;
        }
        var row2 = new ZBROW();
        row2.xh = xh;
        xh++;
        row2.gdh = "*";
        row2.y = kqfws[curpos];
        row2.x = kqfws[curpos + 1];
        row2.ktbs = kqfws[curpos + 2];
        row2.xz = kqfws[curpos + 3];
        curpos += 4;
        list[list.length] = row2;
        ds = 0;
        listqk[qkcount] = list;
        if (curpos != kqfws.length - 1) {
            ds = parseInt(kqfws[curpos]);
            curpos++;

            list = new Array();
            qkcount++;
        }
    }
    return listqk;

}

function ontest() {
    var qks = CopyQKS(m_mqks);
    for (var i = 0; i < qks.length; i++) {
        qks[i].length -= 1;
    }
    for (var i = 0; i < qks.length; i++) {
        for (var j = 0; j < qks[i].length; j++) {
            qks[i][j].x = parseFloat(qks[i][j].x) + 100;
        }
    }
   // sfmap.addGeometryEx(qks, "blue", "kqfw2");
}

function CopyQKS(inqks) {
    var outqks = new Array();
    for (var i = 0; i < inqks.length; i++) {
        var outzbs = new Array();
        for (var j = 0; j < inqks[i].length; j++) {
            var row = new ZBROW();
            row.gdh = inqks[i][j].gdh;
            row.y = inqks[i][j].y;
            row.x = inqks[i][j].x;
            row.ktbs = inqks[i][j].ktbs;
            row.xz = inqks[i][j].xz;
            outzbs[outzbs.length] = row;
        }
        outqks[outqks.length] = outzbs;
    }
    return outqks;
}
function saveZB() {
    var str = "采深上限,采深下限,矿区面积"; //,CKQSYF
    if (m_obj != null) {
        var strs = str.split(',');
        for (i = 0; i < strs.length; i++) {
            m_obj.SetValue(strs[i], $("[name='txt" + strs[i] + "']").val() == null ? "" : $("[name='txt" + strs[i] + "']").val());
        }
        var strzb = GetSTRZB();
        m_obj.SetValue("区域坐标", strzb);
        alert(m_obj.Update());
    }
}
function GetSTRZB() {
    //    var zbs=GetZbFromTable();
    //    var str="1,"+zbs.length +",";
    //    var i=0;
    //    for(i=0;i<zbs.length;i++)
    //    {
    //        str+=(i+1)+","+zbs[i].y+","+zbs[i].x+",";
    //    }
    //    str+=$("[name='txt采深上限']").val()+",";
    //    str+=$("[name='txt采深下限']").val()+",,";
    //    str+="1";
    //    return str;
    var str = "";
    if (m_mqks.length > 0) {
        str = m_mqks.length + ",";
        for (var i = 0; i < m_mqks.length; i++) {
            str += m_mqks[i].length - 1 + ",";
            for (var j = 0; j < m_mqks[i].length - 1; j++) {
                str += m_mqks[i][j].xh + "," + m_mqks[i][j].y + "," + m_mqks[i][j].x + ",";
            }
            str += m_mqks[i][m_mqks[i].length - 1].y + "," + m_mqks[i][m_mqks[i].length - 1].x + "," + m_mqks[i][m_mqks[i].length - 1].ktbs + "," + m_mqks[i][m_mqks[i].length - 1].xz + ",";
        }
        str = str.substr(0, str.length - 1);
    }
    return str;
}

function ZBROW() {
    this.xh = '';
    this.x = '';
    this.y = '';
    this.ktbs = '';
    this.xz = '';
}
//添加坐标
function addzbd() {
    var count = $("tr[tag='useradd']").length;
    count = count + 1;

    var str = '<tr tag="useradd"><td >' + count + '</td><td ><input value="0" class="txtinput2" /></td>\
                                <td><input value="0"  class="txtinput2" /></td></tr>';
    $("#id_zb_table").append(str);
    $("#id_zb_table tr[tag='useradd']").click(function () { $("#id_zb_table tr.selected").removeClass("selected"); $(this).addClass("selected"); });
}
//删除坐标
function deletezbd() {
    if ($("#id_zb_table tr.selected").length > 0) {
        $("#id_zb_table tr.selected").remove();
        $("#id_zb_table tr[tag='useradd']").each(function (index, obj) { $(obj).find("td:first").text(index + 1); });
    }
    else {
        alert("请选中一行");
    }
}
//上图按钮事件
function shangtu() {
    var zbs = GetZbFromTable();
    if ($("#id_sel_tyfs option:selected").attr("tag") == "3dd") {
        zbs = GetConvertZBC(zbs, IServerUrl);
    }
    sfmap.addGeometry(zbs);
}
//从表获取坐标数组
function GetZbFromTable() {
    var zbs = new Array();
    $("#id_zb_table tr[tag='useradd']").each(function (index, obj) { var x = $(obj).find("input:first").val(); var y = $(obj).find("input:last").val(); var row = new ZBROW(); row.x = x; row.y = y; zbs[index] = row; });
    return zbs;
}

//以下为编辑图形部分
function EditZB() {
    var strhtml = "<div id='id_div_kqfw_edit' style='border:2px solid rgb(163, 186, 233);z-index:10001;position:absolute;left:80px;top:100px;width:820px;height:470px; background-color:rgb(200, 214, 233);padding-top:5px'>\
                <p style='font-weight:bold;text-align:center; font-size:18px; margin-top:10px;'>矿区坐标</p><p style='text-align:center;margin:0 auto;color:red'>每个地块以*作为拐点号结束</p><div style='overflow:auto;height:370px;width:100%;'>\
                <table class='zbtable' id='id_kqfw_edit_table' style='cursor:pointer;width:100%;text-align:center;border-collapse:collapse'><tbody><tr style='font-weight:bold;'>\
                <td style='background: rgb(123, 151, 198);width:40px'>序号</td><td style='background: rgb(123, 151, 198);width:40px'>拐点号</td>\
                <td style='background: rgb(123, 151, 198);width:40px'>Y(起标高)</td><td style='background: rgb(123, 151, 198);width:40px'>\
                X(止标高)</td><td style='background: rgb(123, 151, 198);width:40px'>框体标识</td><td style='background: rgb(123, 151, 198);width:40px'>\
                性质</td></tr>\
                </tbody></table></div><button style='margin-left:500px;' onclick='deleteRow()' class='m_btn btn'>删除行</button> <button style='margin-left:20px;' onclick='saveEditZB()' class='m_btn btn'>完成</button>\
        <button style='margin-left:20px;' class='m_btn btn' onclick='cancelEdit()'>取消</button></div>";
    $("#id_main_div").append(strhtml);
    var count = 1;
    if (m_mqks != null && m_mqks.length > 0) {
        for (var i = 0; i < m_mqks.length; i++) {
            for (var j = 0; j < m_mqks[i].length; j++) {
                var html = '<tr onkeyup="" tag="useradd"><td>' + count + '</td><td><input style="width:120px;height:20px;text-align:center"  onblur="funblur(this)" value="' + m_mqks[i][j].gdh + '"/></td><td><input style="width:120px;height:20px;"  value="' + m_mqks[i][j].y + '"/></td><td><input style="width:120px;height:20px;"  value="' + m_mqks[i][j].x + '"/></td>\
                   <td><input  style="width:120px;height:16px;" value="' + m_mqks[i][j].ktbs + '"/></td><td><input style="width:120px;height:14px;"  value="' + m_mqks[i][j].xz + '"/></td></tr>';
                $("#id_kqfw_edit_table").append(html);
                count++;
            }
        }
    }
    $("#id_kqfw_edit_table").append("<tr tag='useradd' onkeyup=''><td>" + $("#id_kqfw_edit_table tr").length + "</td><td><input style='width:120px;height:14px;'  onblur='funblur(this)' /></td><td><input style='width:120px;height:14px;'  onblur='funblur(this)'  /></td><td>\
                <input style='width:120px;height:14px;' onblur='funblur(this)' /></td><td><input style='width:120px;height:14px;'  onblur='funblur(this)'  /></td><td><input  style='width:120px;height:14px;'  onblur='funblur(this)' /></td></tr>");
    $("#id_kqfw_edit_table tr[tag='useradd']").click(function () { $("#id_kqfw_edit_table tr.selected").removeClass("selected"); $(this).addClass("selected"); });

}
function cancelEdit() {
    $("#id_div_kqfw_edit").remove();
}
function funblur(e) {
    if (e.value != "" && e.parentNode.parentNode.rowIndex == $("#id_kqfw_edit_table tr").length - 1) {
        var length = $("#id_kqfw_edit_table tr").length;
        var html = '<tr tag="useradd" onkeyup=""><td>' + length + '</td><td><input  style="width:120px;height:14px;"  onblur="funblur(this)" /></td><td><input  onblur="funblur(this)"   style="width:120px;height:14px;" /></td><td><input   onblur="funblur(this)"  style="width:120px;height:14px;" /></td>\
                   <td><input  onblur="funblur(this)"  style="width:120px;height:14px;"  /></td><td><input  onblur="funblur(this)"  style="width:120px;height:14px;"  /></td></tr>';
        $("#id_kqfw_edit_table").append(html);
        $("#id_kqfw_edit_table tr[tag='useradd']").click(function () { $("#id_kqfw_edit_table tr.selected").removeClass("selected"); $(this).addClass("selected"); });

    }
}
var indexfirst = 0;
var bsucceed = true;
function saveEditZB() {
    //行数必须大于4
    if ($("#id_kqfw_edit_table tr").length < 4) {
        alert("坐标点数必须大于三个");
        return;
    }
    indexfirst = 0;
    bsucceed = true;
    //检查每一行中的各项值添加是否正确
    $("#id_kqfw_edit_table tr[tag=useradd]").each(funeach);

    //存到m_mqks里面
    if (bsucceed == true) {
        var rows = $("#id_kqfw_edit_table tr[tag=useradd]");
        m_mqks.length = 0;
        var zbs = new Array();
        for (var i = 0; i < rows.length; i++) {
            var ele = rows[i];
            var zbrow = new ZBROW();
            var value = $(ele).find("td:eq(1) input:first").val();
            var value1 = $(ele).find("td:eq(2) input:first").val();
            var value2 = $(ele).find("td:eq(3) input:first").val();
            var value3 = $(ele).find("td:eq(4) input:first").val();
            var value4 = $(ele).find("td:eq(5) input:first").val();
            zbrow.xh = value;
            zbrow.gdh = value;
            zbrow.y = value1;
            zbrow.x = value2;
            zbrow.ktbs = value3;
            zbrow.xz = value4;
            zbs[zbs.length] = zbrow;
            if (value == "*") {
                m_mqks[m_mqks.length] = zbs;
                zbs = new Array();
            }
        }
        var qks = CopyQKS(m_mqks); //.myclone();
        addToTableAndMap(qks);
        $("#id_div_kqfw_edit").remove();
    }
}
function funeach() {
    //alert($(this).find("td:eq(1) input:first").val());
    var rowindex = this.rowIndex;
    var value = $(this).find("td:eq(1) input:first").val();
    var value1 = $(this).find("td:eq(2) input:first").val();
    var value2 = $(this).find("td:eq(3) input:first").val();
    if (rowindex != $("#id_kqfw_edit_table tr").length - 1) {
        if (value == null || value == "") {

            alert("第" + rowindex + "行有未填项"); bsucceed = false;
            return false;
        } else if (value == "*") {
            if (rowindex - indexfirst < 4) {
                alert("每个地块的拐点数不得小于三个"); bsucceed = false; return false;
            }
            indexfirst = rowindex;
            var value3 = $(this).find("td:eq(4) input:first").val();
            var value4 = $(this).find("td:eq(5) input:first").val();
            if (value1 == null || value1 == "" || value2 == null || value2 == "" || value3 == null || value3 == "" || value4 == null || value4 == "") {
                alert("第" + rowindex + "行有未填项"); bsucceed = false; return false;
            }

        }
        else {
            if (value1 == null || value == "" || value2 == null || value2 == "") {
                alert("第" + rowindex + "行有未填项"); bsucceed = false; return false;
            }
        }

    }
}
function deleteRow() {
    $("#id_kqfw_edit_table tr.selected").remove();
    var i = 1;
    $("#id_kqfw_edit_table tr[tag=useradd]").each(function () {
        $(this).find("td:first").text(i); i++;
    });
}


var xmlhttp;
function afterConvert(queryEventArgs) {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var responseText = xmlhttp.responseText;
            if (responseText != null && responseText != "") {
                var obj = JSON.parse(responseText);
                if (obj != null) {
                    m_qks = obj.parts;
                    sfmap.addGeometryEx(m_qks, "", "tkqkqfw");
                }
            }
        }
    }
}
function callJavaService(url, callback, data) {


    //0 获取数据
    var name = "ccc";
    //1创建

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType("text/xml");
        }
    }
    else if (window.ActiveXObject) {
        var activexName = ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
        for (var i = 0; i < activexName.length; i++) {
            try {
                xmlhttp = new ActiveXObject(activexName[i]);
                break;
            }
            catch (e) {
            }
        }
    }

    //2  回调
    xmlhttp.onreadystatechange = callback;

    //3 open
    xmlhttp.open("POST", url, true);

    // xmlhttp.setRequestHeader("Content-Length",1000);
    xmlhttp.setRequestHeader("CONTENT-TYPE", " text/plain;charset=UTF-8");
    var str = "115.2963,25.8337;115.2888,25.8314;115.2876,25.8258;115.2947,25.823;115.297,25.8222;115.3005,25.8243;115.3021,25.8269;115.3014,25.8313;115.2963,25.8337&TDLY&LYK_DLTB";
    //4 发送数据
    xmlhttp.send(data);

    //回调函数 

}